package sgt.onlinetest;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import org.securityfilter.realm.SimpleSecurityRealmBase;
import sgt.onlinetest.entity.*;
import sgt.onlinetest.hibernatedao.HibernateDAOFactory;

/**
 *
 * @author Administrator
 */
public class SecurityRealm extends SimpleSecurityRealmBase {

    @Override
    public boolean booleanAuthenticate(String username, String password) {
        HibernateDAOFactory factory = new HibernateDAOFactory();
        User user = factory.getUserDAO().getByUsername(username);
        if (user == null) {
            return false;
        }
        return user.getPassword().equals(password);
    }

    @Override
    public boolean isUserInRole(String username, String rolename) {
        HibernateDAOFactory factory = new HibernateDAOFactory();
        User user = factory.getUserDAO().getByUsername(username);
        if (user == null) {
            return false;
        }
        for (Role r : user.getRoles()) {
            if (r.getName().equals(rolename)) {
                return true;
            }
        }
        return false;
    }
}
